
/*
* Copyright (c) Z.G. Shi  <blueflycn at gmail dot com>
* Make a better world~
*/

#ifndef _TYAN_H_
#define _TYAN_H_

#define  TYN_OK          0
#define  TYN_ERROR      -1
#define  TYN_AGAIN      -2
#define  TYN_BUSY       -3
#define  TYN_DONE       -4
#define  TYN_DECLINED   -5
#define  TYN_ABORT      -6

#define TYN_TYH_SIGN 3014996245u
#define TYN_TYD_SIGN 2193362704u
#define TYN_TYF_SIGN 880896166u
#define TYN_TYP_SIGN 1793464614u
#define TYN_TYI_SIGN 1160260045u
#define TYN_TERM32_T 447261893u
#define TYN_TERM64_T 583605499u
#define TYN_DOC_ID_T 1366605920u
#define TYN_ATTR32_T 4142741565u
#define TYN_ATTR64_T 2566338070u
#define TYN_DOC_ID32_T 333500118u
#define TYN_DOC_ID64_T 725228218u
#define TYN_FULLTEXT_T 2927821541u
#define TYN_ATTR_INT_T 2017142929u
#define TYN_ATTR_UINT8_T 2434595528u
#define TYN_ATTR_UINT8_T 2434595528u
#define TYN_ATTR_UINT16_T 1509563817u
#define TYN_ATTR_UINT32_T 1906660024u
#define TYN_ATTR_UINT64_T 2517593582u
#define TYN_ATTR_INT8_T 2262032730u
#define TYN_ATTR_INT16_T 661420903u
#define TYN_ATTR_INT32_T 3056706098u
#define TYN_ATTR_INT64_T 2492866796u
#define TYN_ATTR_FLOAT_T 1434789291u
#define TYN_ATTR_DOUBLE_T 728519693u
#define TYN_ATTR_TIMESTAMP_T 2716886915u
#define TYN_ATTR32_INT_T 3194280382u
#define TYN_ATTR32_UINT8_T 3861526474u
#define TYN_ATTR32_UINT16_T 1210162468u
#define TYN_ATTR32_UINT32_T 2401097819u
#define TYN_ATTR32_UINT64_T 2370659770u
#define TYN_ATTR32_INT8_T 1409884953u
#define TYN_ATTR32_INT16_T 3505991270u
#define TYN_ATTR32_INT32_T 114773903u
#define TYN_ATTR32_INT64_T 1149229065u
#define TYN_ATTR32_FLOAT_T 842990350u
#define TYN_ATTR32_DOUBLE_T 2361825051u
#define TYN_ATTR32_TIMESTAMP_T 2883218512u
#define TYN_ATTR64_INT_T 2587037056u
#define TYN_ATTR64_UINT8_T 3882414985u
#define TYN_ATTR64_UINT16_T 2344641348u
#define TYN_ATTR64_UINT32_T 2215177807u
#define TYN_ATTR64_UINT64_T 2805213485u
#define TYN_ATTR64_INT8_T 1881842313u
#define TYN_ATTR64_INT16_T 825072426u
#define TYN_ATTR64_INT32_T 3724274747u
#define TYN_ATTR64_INT64_T 2095277009u
#define TYN_ATTR64_FLOAT_T 1905919462u
#define TYN_ATTR64_DOUBLE_T 989253787u
#define TYN_ATTR64_TIMESTAMP_T 4269878518u




#include <stdint.h>
#include <fcntl.h>
#include "tyn_utils.h"
#include "uthash.h"
#include "libconfig/libconfig.h"


typedef struct tyn_config_indexer_t {
    char *dl_directory;
    size_t memory_limit;
} tyn_config_indexer_t;

typedef struct tyn_config_searchd_t {
    char *host;
    int port;
    int max_match;
} tyn_config_searchd_t;

typedef struct tyn_config_index_t {
    char *index_name;
    uint32_t index_name_hash;
    char *path;
    
    char *source_name;
    char *reader_path;
    config_setting_t *reader_params;
    
    char *tokenizer_path;
    config_setting_t *tokenizer_params;
    
    char* doc_id_field;
    unsigned int doc_id_field_type;
    unsigned int doc_id_field_hash;
    
    char **fulltext_fields;
    size_t num_fulltext_fields;
    unsigned int *fulltext_field_hashes;
    
    char **attr_int8_t_fields;
    size_t num_attr_int8_t_fields;
    unsigned int *attr_int8_t_field_hashes;
    
    char **attr_uint8_t_fields;
    size_t num_attr_uint8_t_fields;
    unsigned int *attr_uint8_t_field_hashes;

    char **attr_int16_t_fields;
    size_t num_attr_int16_t_fields;
    unsigned int *attr_int16_t_field_hashes;

    char **attr_uint16_t_fields;
    size_t num_attr_uint16_t_fields;
    unsigned int *attr_uint16_t_field_hashes;

    char **attr_int32_t_fields;
    size_t num_attr_int32_t_fields;
    unsigned int *attr_int32_t_field_hashes;

    char **attr_uint32_t_fields;
    size_t num_attr_uint32_t_fields;
    unsigned int *attr_uint32_t_field_hashes;
    
    char **attr_int64_t_fields;
    size_t num_attr_int64_t_fields;
    unsigned int *attr_int64_t_field_hashes;

    char **attr_uint64_t_fields;
    size_t num_attr_uint64_t_fields;
    unsigned int *attr_uint64_t_field_hashes;
    
    char **attr_float_fields;
    size_t num_attr_float_fields;
    unsigned int *attr_float_field_hashes;

    char **attr_double_fields;
    size_t num_attr_double_fields;
    unsigned int *attr_double_field_hashes;

    
    char **attr_timestamp_fields;
    size_t num_attr_timestamp_fields;
    unsigned int *attr_timestamp_field_hashes;
    
    size_t num_fields;
} tyn_config_index_t;

typedef struct tyn_config_t {
    char file_name[128];
    char *file_str;
    size_t file_str_len;
    tyn_config_indexer_t *indexer;
    tyn_config_searchd_t *searchd;
    size_t indexes_count;
    tyn_config_index_t **indexes;
} tyn_config_t;

typedef struct tyn_header_t {
    uint32_t signature;
    uint32_t version;
    size_t config_file_size;
    time_t time_created;
    size_t total_documents;
    size_t total_bytes;
    
} tyn_header_t;

typedef struct tyn_document_t {
    char **fields;
    char **field_names;
    size_t *field_lengths;
    size_t *field_name_lengths;
    size_t num_fields;
    size_t max_num_fields;
} tyn_document_t;

typedef enum tyn_field_type_enum_t {
    TYN_FIELD_ID_T,
    TYN_FIELD_FULLTEXT_T,
    TYN_FIELD_ATTR_INT8_T,
    TYN_FIELD_ATTR_INT16_T,
    TYN_FIELD_ATTR_INT32_T,
    TYN_FIELD_ATTR_INT64_T,
    TYN_FIELD_ATTR_FLOAT_T,
    TYN_FIELD_ATTR_STRING_T,
    TYN_FIELD_ATTR_TIMESTAMP_T,
    TYN_FIELD_ATTR_MVA_T,
    TYN_FIELD_UNDEFINED_T
} tyn_field_type_enum_t;

/*
typedef struct tyn_field_type_t {
    int idx;
    tyn_field_type_enum_t type;
} tyn_field_type_t;
*/
typedef struct __attribute__ ((__packed__)) term32_t {
    uint32_t word_id;
    uint32_t document_id;
    uint32_t field_id;
    uint32_t pos;
} term32_t;
typedef struct __attribute__ ((__packed__)) term64_t {
    uint32_t word_id;
    uint64_t document_id;
    uint32_t field_id;
    uint32_t pos;
} term64_t;

#define TYN_DECLARE_ATTR(BITS, TYPE)    \
typedef struct __attribute__ ((__packed__)) attr##BITS##_##TYPE {     \
    TYPE value;                         \
    uint##BITS##_t document_id;         \
} attr##BITS##_##TYPE;

TYN_DECLARE_ATTR(32, int8_t)
TYN_DECLARE_ATTR(32, int16_t)
TYN_DECLARE_ATTR(32, int32_t)
TYN_DECLARE_ATTR(32, int64_t)
TYN_DECLARE_ATTR(32, float)


TYN_DECLARE_ATTR(64, int8_t)
TYN_DECLARE_ATTR(64, int16_t)
TYN_DECLARE_ATTR(64, int32_t)
TYN_DECLARE_ATTR(64, int64_t)
TYN_DECLARE_ATTR(64, float)

typedef struct attr32_t {
    uint32_t document_id;
    uint32_t unit_idx;
} attr32_t;

typedef struct attr64_t {
    uint64_t document_id;
    uint64_t unit_idx;
} attr64_t;

typedef struct attr32_context_t {
    int fd_attrs32_unsorted;
    int fd_attrs32;
    size_t attrs_length;
    char *buffer;
    size_t buffer_size;
} attr32_context_t;

typedef struct term32_index_entry_t {
    //uint32_t word_id;
    uint32_t num_encoded_doc;
    uint32_t num_encoded_freq;
    uint32_t num_encoded_pos;
    //off_t offset_doc;
    //off_t offset_freq;
    //off_t offset_pos;
    //off_t offset_attr;
    uint32_t size_encoded_doc;
    uint32_t size_encoded_freq;
    uint32_t size_encoded_pos;
    //size_t size_attr;
    
} term32_index_entry_t;

typedef struct term32_index_t {
    char *str;
    uint32_t str_len;
    off_t offset_doc;
    off_t offset_freq;
    off_t offset_pos;
    uint32_t num_encoded_doc;
    uint32_t num_encoded_freq;
    uint32_t num_encoded_pos;
    uint32_t size_encoded_doc;
    uint32_t size_encoded_freq;
    uint32_t size_encoded_pos;
    
    
    UT_hash_handle hh;
} term32_index_t;

typedef struct term32_entity_t {
    char *str;
    uint32_t str_len;
    uint32_t id;
    term32_index_entry_t index;
    UT_hash_handle hh;
    UT_hash_handle hh2;
} term32_entity_t;

typedef struct term32_context_t {
    int fd_terms32;
    int fd_doc;
    int fd_freq;
    int fd_pos;
    int is_first;
    size_t field_idx;
    size_t size_buffer_doc;
    size_t size_buffer_freq;
    size_t size_buffer_pos;
    size_t size_buffer_encode;
    size_t size_buffer_write_doc;
    size_t size_buffer_write_freq;
    size_t size_buffer_write_pos;
    char *buffer_doc;
    char *buffer_freq;
    char *buffer_pos;
    char *buffer_encode;
    char *buffer_write_doc;
    char *buffer_write_freq;
    char *buffer_write_pos;
    char *buffer_write_ptr_doc;
    char *buffer_write_ptr_freq;
    char *buffer_write_ptr_pos;
    char *buffer_write_max_doc;
    char *buffer_write_max_freq;
    char *buffer_write_max_pos;
    
    uint32_t last_document_id;
    uint32_t last_word_id;
    uint32_t last_field_id;
    uint32_t num_max_pos;
    uint32_t num_max_freq;
    uint32_t num_max_doc;
    uint32_t num_doc;
    uint32_t num_freq;
    uint32_t num_pos;
    uint32_t *doc_ptr;
    uint32_t *freq_ptr;
    uint32_t *pos_ptr;
    
    off_t offset_doc;
    off_t offset_freq;
    off_t offset_pos;
    
    size_t num_docid;
    size_t num_wordid;
    
    uint32_t *fulltext_field_hashes;
    size_t num_fulltext_fields;
    
    int is_new_word_id;
    int is_new_document_id;
    int is_new_field_id;
    
    term32_entity_t *term32_entities;
    term32_entity_t *term32_entity;
} term32_context_t;

typedef struct tyn_reader_t {
    tyn_document_t *document;
    void *context;
} tyn_reader_t;

typedef struct tyn_token_t{
    char *str;
    size_t str_len;
    size_t pos;
} tyn_token_t;

typedef struct tyn_tokenizer_t {
    tyn_token_t *token;
    void *context;
} tyn_tokenizer_t;
#endif /* _TYAN_H_ */
